/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.cshoo.tattoo.authorize.api.dto.UserResp;
import org.cshoo.tattoo.authorize.app.UserAppService;
import org.cshoo.tattoo.authorize.domain.po.User;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.cshoo.tattoo.embed.infra.exception.ValidateException;
import org.cshoo.tattoo.embed.infra.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/user")
@Validated
public class UserController {

    @Resource
    private UserAppService userAppService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response<?> addUser(@RequestBody User user) {
        log.info("user:{}", user);
        userAppService.addUser(user);
        return Response.builder().build();
    }

    @RequestMapping(value = "/resetPassword", method = RequestMethod.PUT)
    public Response<?> resetPassword(@RequestParam("userId") Long userId) {
        log.info("userId:{}", userId);
        userAppService.resetPassword(userId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/modifyPassword", method = RequestMethod.PUT)
    public Response<?> modifyPassword(@RequestParam("oldPassword") @NotBlank(message = "oldPassword is required") String oldPassword, @RequestParam("newPassword") @NotBlank(message = "newPassword is required") String newPassword) throws ValidateException {
        userAppService.modifyPassword(oldPassword, newPassword);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Response<?> updateUser(@RequestBody User user) {
        log.info("user:{}", user);
        userAppService.updateUser(user);
        return Response.builder().build();
    }

    @RequestMapping(value = "/all/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public Page<User> findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return userAppService.findAllUser(pageNum, pageSize);
    }

    @RequestMapping(value = "/findUsersByGroupId/{groupId}", method = RequestMethod.GET)
    public List<User> findUsersByGroupId(@PathVariable("groupId") Long groupId) {
        log.info("groupId:{}", groupId);
        return userAppService.findUsersByGroupId(groupId);
    }

    @RequestMapping(value = "/findUserByName", method = RequestMethod.GET)
    public List<User> findUserByName(String name) {
        log.info("name:{}", name);
        return userAppService.findUserByName(name);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Response<?> deleteUsers(@RequestBody List<Long> ids) {
        userAppService.deleteUsers(ids);
        return Response.builder().build();
    }

    @RequestMapping(value = "/assignGroup", method = RequestMethod.POST)
    public Response<?> assignGroup(Long userId, Long groupId) {
        log.info("userId:{}, groupId:{}", userId, groupId);
        userAppService.assignGroup(userId, groupId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/authorizeMenu", method = RequestMethod.POST)
    public Response<?> authorizeMenu(@RequestParam("userId") Long userId, @RequestParam("menuId") Long menuId) {
        log.info("userId:{}, menuId:{}", userId, menuId);
        userAppService.authorizeMenu(userId, menuId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/cancelMenuAuthority", method = RequestMethod.DELETE)
    public Response<?> cancelMenuAuthority(@RequestParam("userId") Long userId, @RequestParam("menuId") Long menuId) {
        log.info("userId:{}, menuId:{}", userId, menuId);
        userAppService.cancelMenuAuthority(userId, menuId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/authorizeButton", method = RequestMethod.POST)
    public Response<?> authorizeButton(@RequestParam("userId") Long userId, @RequestParam("buttonId") Long buttonId) {
        log.info("userId:{}, menuId:{}", userId, buttonId);
        userAppService.authorizeButton(userId, buttonId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/getCurrentUserInfo", method = RequestMethod.GET)
    public UserResp getCurrentUserInfo() {
        UserResp userResp = new UserResp();
        BeanUtils.copyProperties(UserUtils.getUser(), userResp);
        if (StringUtils.isBlank(userResp.getName())) {
            userResp.setName(userResp.getUsername());
        }
        return userResp;
    }
}
